'use strict';
import Base from './base.js';
//美易通订单类控制器请求方法
module.exports = class extends Base {
  async getyejiAction () {
    let data = this.post()
    // 获取机构数据
    let departmentid = await this.model('or_department').where({floor: 0, orcode: data.orcode}).getField('id', true)
    // 获取机构下的分店
    let shoplist = await this.model('or_department').where({pid: departmentid}).select()

    if (shoplist.length > 0) {
      for (var i = shoplist.length - 1; i >= 0; i--) {
        let o = {xj: 0, kh: 0}
        let yj = await this.model('or_shop_yejiticheng')
        .where({shopid: shoplist[i].id, addtime: {'>=': data.startTime, '<=': data.endTime}})
        .field('sum(xianjinyeji) as xj, sum(kahaoyeji) as kh')
        .group('shopid')
        .find()
        shoplist[i].yj = Object.assign({}, o, yj)
        // 业绩分类
        // 获取本部业绩
        let benbu = await this.model('or_shop_yejiticheng')
        .where({shopid: shoplist[i].id, bumenid: 0, xiaozuid: 0, addtime: {'>=': data.startTime, '<=': data.endTime}})
        .field('sum(xianjinyeji) as xj, sum(kahaoyeji) as kh')
        .group('shopid')
        .find()
        shoplist[i].benbu = Object.assign({}, o, benbu)
        shoplist[i].zhishu = {xj: shoplist[i].yj.xj - shoplist[i].benbu.xj, kh: shoplist[i].yj.kh - shoplist[i].benbu.kh}


      }
    }
    // let list = await this.model('or_shop_yejiticheng').where({id:})
    return this.json(shoplist)
  }

  // 获取本门店家小组的直属业绩
  async getzhishuyejiAction () {
    let data = this.get()
    // 获取指数业绩列表
    let list = await this.model('or_shop_yejiticheng')
    .alias('y')
    .join('`think_or_shop_order` o on y.orderid = o.id')
    .where({'y.shopid': data.shopid, 'y.bumenid': data.bumenid, 'y.xiaozuid': data.xiaozuid, addtime: {'>=': data.timeStartUinx, '<=': data.timeEndUinx}})
    .select()



    return this.json(list)
  }

  async getbumenyejiAction () {
    let data = this.get()
    // 取出部门列表
    let list = await this.model('or_department').where({pid: data.shopid}).select()

    if (list.length > 0) {
      for (var i = list.length - 1; i >= 0; i--) {
        let o = {xj: 0, kh: 0}
        let yj = await this.model('or_shop_yejiticheng')
        .where({bumenid: list[i].id, addtime: {'>=': data.timeStartUinx, '<=': data.timeEndUinx}})
        .field('sum(xianjinyeji) as xj, sum(kahaoyeji) as kh')
        .group('bumenid')
        .find()
        list[i].yj = Object.assign({}, o, yj)

        let benbu = await this.model('or_shop_yejiticheng')
        .where({shopid: data.shopid, bumenid: list[i].id, xiaozuid: 0, addtime: {'>=': data.timeStartUinx, '<=': data.timeEndUinx}})
        .field('sum(xianjinyeji) as xj, sum(kahaoyeji) as kh')
        .group('bumenid')
        .find()
        list[i].benbu = Object.assign({}, o, benbu)
        list[i].zhishu = {xj: list[i].yj.xj - list[i].benbu.xj, kh: list[i].yj.kh - list[i].benbu.kh}
      }
    }

    return this.json(list)
  }

  // 小组业绩
  async getxiaozuyejiAction () {
    let data = this.get()
    let list = await this.model('or_department').where({pid: data.bumenid}).select()

    if (list.length > 0) {
      for (var i = list.length - 1; i >= 0; i--) {
        let o = {xj: 0, kh: 0}
        let yj = await this.model('or_shop_yejiticheng')
        .where({xiaozuid: list[i].id, addtime: {'>=': data.timeStartUinx, '<=': data.timeEndUinx}})
        .field('sum(xianjinyeji) as xj, sum(kahaoyeji) as kh')
        .group('xiaozuid')
        .find()
        list[i].yj = Object.assign({}, o, yj)

        // 取得业绩前三名人员

        let benbu = await this.model('or_shop_yejiticheng')
        .alias('y')
        .join('`think_or_app_user` u on y.userid = u.id')
        .where({'y.shopid': data.shopid, 'y.bumenid': data.bumenid, 'y.xiaozuid': list[i].id, 'y.addtime': {'>=': data.timeStartUinx, '<=': data.timeEndUinx}})
        .field('u.id, u.name, u.headurl, sum(xianjinyeji) as xj, sum(kahaoyeji) as kh, sum(xianjinyeji + kahaoyeji) as zj')
        .group('userid')
        .order('zj DESC')
        .select()
        list[i].benbu = benbu
      }
    }
    return this.json(list)
  }

  async getshopyejiAction () {
    let data = this.post()
    let yj = await this.model('or_shop_yejiticheng')
    .where({shopid: data.shopid, addtime: {'>=': data.startTime, '<=': data.endTime}})
    .field('sum(xianjinyeji) as xj, sum(kahaoyeji) as kh')
    .group('shopid')
    .find()
    // 业绩分类
    // 获取本部业绩
    let benbu = await this.model('or_shop_yejiticheng')
    .where({shopid: data.shopid, bumenid: 0, xiaozuid: 0, addtime: {'>=': data.startTime, '<=': data.endTime}})
    .field('sum(xianjinyeji) as xj, sum(kahaoyeji) as kh')
    .group('shopid')
    .find()

    benbu.xj = benbu.xj || 0
    benbu.kh = benbu.kh || 0

    return this.json({
        zhishu: {xj: yj.xj - benbu.xj || 0, kh: yj.kh - benbu.kh || 0},
        benbu: benbu
    })
  }
}